# 指定 cmake 最小版本, 意指 cmake 版本低于指定版本则构建过程终止
cmake_minimum_required(VERSION 3.26)

# 语言环境配置 ###########################################################################################################
# 语言版本
set(CMAKE_CXX_STANDARD 14)

# 如果指定的语言版本不受支持, 则构建过程终止
set(CMAKE_CXX_STANDARD_REQUIRED True)

# 只弃用 ISO C++ 标准的编译器标志, 而不使用特定编译器的扩展
set(CMAKE_CXX_EXTENSIONS OFF)

# 将 install_rpath 的设置应用在 build_rpath 上
# 避免在开发期间出现动态库找不到的问题
set(CMAKE_BUILD_WITH_INSTALL_RPATH True)

# 项目配置 ##############################################################################################################
project(graver_test CXX)

# 编译相关配置 ###########################################################################################################
option(ENABLE_COVERAGE "gen coverage" OFF)

# 测试覆盖率选项
if(ENABLE_COVERAGE)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
endif()

# 生成 compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# vcpkg find package ###################################################################################################
# fmt 库
find_package(fmt CONFIG REQUIRED)

# spdlog 库
find_package(spdlog CONFIG REQUIRED)

# 项目生成配置 ###########################################################################################################
aux_source_directory(. test_srcs)
add_executable(${PROJECT_NAME} ${test_srcs})

target_include_directories(${PROJECT_NAME} PRIVATE
    ${PROJECT_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/third
)

target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt ${lib_person_name} ${lib_util_name})

set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test)

set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH "\${ORIGIN}/../lib")

# 测试相关配置 ###########################################################################################################
# 执行所有测试
# ctest --test-dir build
add_test(NAME test_all COMMAND ${PROJECT_NAME})
set_tests_properties(test_all PROPERTIES WORKING_DIRECTORY ${dist_dir}/test)

# test add_custom_command
# ctest --test-dir build -R test_common
add_test(NAME test_common COMMAND ${PROJECT_NAME} -ts=test_common)
set_tests_properties(test_common PROPERTIES WORKING_DIRECTORY ${dist_dir}/test)